package com.whw.springboot.walk.service.servicelmpl;

import com.whw.springboot.walk.entity.Action;
import com.whw.springboot.walk.entity.ActionNumber;
import com.whw.springboot.walk.entity.Apply;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.mapper.ActionMapper;
import com.whw.springboot.walk.mapper.ApplyMapper;
import com.whw.springboot.walk.service.ActionService;
import com.whw.springboot.walk.utils.ActionUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ActionServiceImpl implements ActionService {

    @Resource
    private ActionMapper actionMapper;
    @Resource
    private ApplyMapper applyMapper;

    @Override
    public Message<?> insertAction(ActionNumber actionNumber) throws ParseException {
        return actionMapper.insertAction(ActionUtil.change(actionNumber))>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> insertActionFile(ActionNumber actionNumber) throws ParseException {
        return actionMapper.insertActionFile(actionNumber)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> deleteActionByPrimaryKey(String actId) {
        return actionMapper.deleteActionByPrimaryKey(actId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> updateActionByPrimaryKey(ActionNumber actionNumber) throws ParseException {
        return actionMapper.updateActionByPrimaryKey(ActionUtil.change(actionNumber))>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<Action> findActionByPrimaryKey(String actId) {
        return Message.success(null).add(actionMapper.findActionByPrimaryKey(actId));
    }

    @Override
    public Message<List<Action>> findActionsByType(String actType) {
        return Message.success(null).add(actionMapper.findActionsByType(actType));
    }

    @Override
    public Message<List<Action>> findAllActions() {
        return Message.success(null).add(actionMapper.findAllActions());
    }

    @Override
    public Message<List<Action>> findActionByTitle(String actTitle) {
        return Message.success(null).add(actionMapper.findActionByTitle(actTitle));
    }

    @Override
    public Message<?> adminManageAction(ActionNumber actionNumber) throws ParseException {
        return actionMapper.updateActionByPrimaryKey(ActionUtil.change(actionNumber))>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message<List<Action>> findOneUserAllActions(String userId) {
        List<Apply> applies = applyMapper.selectAllApplyByUserId(userId);
        List<Action> actions = new ArrayList<>();
        for (Apply  apply:applies) {
            Action action = actionMapper.findActionByPrimaryKey(apply.getApplyActId());
            actions.add(action);
        }
        return Message.success(null).add(actions);
    }
}
